package com.android.happymall.fragment.group;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.happymall.model.group.MainList;
import com.android.happymall.utils.ObjectUtils;
import com.android.happymall.view.recycleview.CustomLoadMoreView;
import com.android.happymall.adapt.MainlistAdapt;
import com.android.happymall.model.group.MainListCallback;
import com.android.happymall.utils.ConstantsUrl;
import com.android.happymall.utils.HttpUtils;
import com.android.happymall.utils.MyLog;
import com.android.happymall.R;
import com.android.happymall.fragment.BaseLazyFragment;
import com.android.happymall.view.recycleview.CustomRecycleView;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;

/**
 * 热搜页
 * ---------------------------------------
 */
public class Fragment_TabRecommendList extends BaseLazyFragment implements  BaseQuickAdapter.RequestLoadMoreListener {


    @BindView(R.id.main_top_bg)
    Toolbar mainTopBg;
    @BindView(R.id.listView)
    CustomRecycleView mRecyclerView;
    @BindView(R.id.swipe_refresh_layout)
    SwipeRefreshLayout mRefreshLayout;
    @BindView(R.id.click_resetnetwork)
    LinearLayout clickResetnetwork;
    @BindView(R.id.progress)
    RelativeLayout progress;
    @BindView(R.id.no_data_tv)
    TextView noDataTv;
    @BindView(R.id.no_data)
    RelativeLayout noData;
    @BindView(R.id.progress_loading)
    FrameLayout progressLoading;
    private View rootView;
    private MainlistAdapt adapter;
    private LayoutInflater inflater;
    private boolean networkConnected;
    private FragmentActivity mcontext;
    private static final String ARG_PARAM1 = "param1";
    private String mParam1;
    private int page = 1;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
        }
    }

    public static Fragment_TabRecommendList newInstance(String param1) {
        Fragment_TabRecommendList fragment = new Fragment_TabRecommendList();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected void initPrepare() {

        //刷新
        ReFreshData();
    }

    private LinearLayoutManager mLayoutManager;
    private GridLayoutManager mGridLayoutManager;

    private void initRecycle() {
        adapter = new MainlistAdapt(mcontext, null, "0");
        adapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        adapter.setLoadMoreView(new CustomLoadMoreView());
        adapter.setOnLoadMoreListener(this, mRecyclerView);
        mLayoutManager = new LinearLayoutManager(mcontext);
        mGridLayoutManager = new GridLayoutManager(mcontext, 2);
        mRecyclerView.setLayoutManager(mLayoutManager);
        mRecyclerView.setAdapter(adapter);
        mRecyclerView.GetToTop((ImageView) rootView.findViewById(R.id.top), 0);
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter1, View view, int position) {
                ObjectUtils.ToDetailActivity(mcontext, 1, adapter.getItem(position).getId(), "");
            }
        });
        adapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public boolean onItemChildClick(BaseQuickAdapter adapter1, View view, int position) {
                switch (view.getId()) {
                    case R.id.main_list_item_go_kai:
                        ObjectUtils.ToDetailActivity(mcontext, 1, adapter.getItem(position).getId(), "");
                        break;

                }
                return false;
            }
        });
    }

    @Override
    protected void onInvisible() {

    }

    @Override
    protected void initData() {

        networkConnected = ObjectUtils.isNetworkConnected(getActivity());
        if (networkConnected) {
            GetListData(page, mParam1);
        } else {

            ObjectUtils.GetDataNet(clickResetnetwork, progress, 4);
        }


    }

    @Override
    protected View initView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        this.inflater = inflater;
        rootView = inflater.inflate(R.layout.layout_recycle_refresh, container, false);
        ButterKnife.bind(this, rootView);
        mcontext = getActivity();
        mainTopBg.setVisibility(View.GONE);
        initRecycle();

        return rootView;
    }

    private void ReFreshData() {
        mRefreshLayout.setColorSchemeResources(R.color.app_title);
        mRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                networkConnected = ObjectUtils.isNetworkConnected(mcontext);
                if (networkConnected) {
                    page = 1;
                    GetListData(page, mParam1);
                } else {
                    ObjectUtils.toast(mcontext, "网络连接出现异常");
                    mRefreshLayout.setRefreshing(false);
                }

            }
        });


    }

    @OnClick(R.id.click_resetnetwork_refresh)
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.click_resetnetwork_refresh:
                ObjectUtils.GetDataNet(clickResetnetwork, progress, 3);
                page = 1;
                GetListData(page, mParam1);
                break;
        }
    }

    @Override
    public void onLoadMoreRequested() {
        page = page + 1;
        GetListData(page, mParam1);
    }

    private void GetListData(final int page, String type) {
        String address = ConstantsUrl.domain + ConstantsUrl.MAIN_LISTDATA + "?page=" + page + type;
        MyLog.e("地址", address);
        networkConnected = ObjectUtils.isNetworkConnected(mcontext);

        if (networkConnected) {
            HttpUtils.get(address, null, new MainListCallback() {

                @Override
                public void onError(Call call, Exception e, int id) {
                    mRefreshLayout.setRefreshing(false);
                    ObjectUtils.GetDataNet(clickResetnetwork, progress, 2);
                }

                @Override
                public void onResponse(MainList response, int id) {
                    mRefreshLayout.setRefreshing(false);
                    ObjectUtils.GetDataNet(clickResetnetwork, progress, 1);

                    if (response.getCode() == 0) {

                        List<MainList.MainListdata.Mainlistdata> list = response.getData().getList();
                        if (list == null && page == 1) {
                            noData.setVisibility(View.VISIBLE);
                            return;
                        }
                        ArrayList<MainList.MainListdata.Mainlistdata> listdata = new ArrayList<>();

                        if (list != null && list.size() > 0) {
                            for (int i = 0; i < list.size(); i++) {
                                listdata.add(new MainList.MainListdata.Mainlistdata(response.getData().getList_type(), list.get(i).getId()
                                        , list.get(i).getTypeid(), list.get(i).getName(), list.get(i).getPrice(), list.get(i).getTeam_price(), list.get(i).getDiscount_price()
                                        , list.get(i).getThumb(), list.get(i).getTeam_num(), list.get(i).getTeam_people_num(), list.get(i).getLuck_num(), list.get(i).getEnd_time()
                                        , list.get(i).getStart_time(), list.get(i).getRun_time()));
                            }
                            if (page == 1) {
                                adapter.setNewData(listdata);
                                if (response.getData().getList_type() == 0) {
                                    mRecyclerView.setLayoutManager(mLayoutManager);
                                    mRecyclerView.setAdapter(adapter);
                                } else {
                                    mRecyclerView.setLayoutManager(mGridLayoutManager);
                                    mRecyclerView.setAdapter(adapter);
                                }
                            } else {
                                adapter.addData(listdata);
                                adapter.loadMoreComplete();
                            }
                        } else {
                            adapter.loadMoreEnd();
                        }
                    } else {
                        noData.setVisibility(View.VISIBLE);
                        noDataTv.setVisibility(View.VISIBLE);
                        noDataTv.setText(response.getMsg());
                    }
                }
            });
        } else {
            if (page == 1) {
                ObjectUtils.GetDataNet(clickResetnetwork, progress, 2);
            }
            ObjectUtils.toast(mcontext, "网络连接出现异常");
            adapter.loadMoreFail();
        }
    }



}
